<?php

namespace App\Http\Service;

use App\Http\Util\Result;
use App\Model\Member;
use App\Model\Operator;
use App\Model\Scenery;
use Illuminate\Support\Facades\Hash;

trait ServiceLogin{

    //普通账号登录
    public function postLogin($account,$password){
        $member = Member::where('account',$account)->first();
        if(!$member){
            return Result::failure('账号不存在');
        }
        if(Hash::check($password,$member->password)){
			if($member['status']==2){
				  return Result::failure('帐号已被管理员禁用');
			}
            session(['member'=>$member]);
            return Result::success('登录成功')->setType(1);
        }
        else{
            return Result::failure('密码错误');
        }
    }

    public function checkLogin(){
        return session('member')?true:false;
    }

    public function getMember(){
        return session('member');
    }

    public function editPwd($password){
        $member = $this->getMember();
        $member->password = bcrypt($password);
        $member->save();
        session(['member'=>$member]);//重新设置session
    }

    /**
     * 景区帐号登录
     * czs
     */
    public function operatorLogin($account,$password) {
        $member = Operator::where('account',$account)->first();
        if(!$member){
            return Result::failure('账号不存在');
        }
        if($member->status==2){
            return Result::failure('账号已被禁用！');
        }
        if($password==$member->password){
            session(['operator'=>$member]);
            return Result::success('登录成功！')->setType(2);
        }
        else{
            return Result::failure('密码错误');
        }
    }

    /**
     * 景区帐号登录接口
     * czs
     */
    public function operatorLoginApi($account,$password) {
        $member = Operator::where('account',$account)->first();
        if(!$member){
            return Result::failure('账号不存在')->setData((object)null)->setType(1);
        }
        if($member->status == 2){
            return Result::failure('账号已被禁用')->setData((object)null)->setType(4);
        }
        if($password==$member->password){
            $data['scenery_id'] = $member->scenery_id;
            $data['name'] = Scenery::where('id',$member->scenery_id)->pluck('name');
            return Result::success('登录成功！')->setData($data)->setType(3);
        }
        else{
            return Result::failure('密码错误')->setData((object)null)->setType(2);
        }
    }
}


